package fun.codedesign.yinxue.code.php2java.service;

import fun.codedesign.yinxue.code.engine.antlr.php.PhpLexer;
import fun.codedesign.yinxue.code.engine.antlr.php.PhpParser;
import fun.codedesign.yinxue.code.php2java.model.Php2JavaContext;
import fun.codedesign.yinxue.code.php2java.service.visitor.Php2JavaStringBuilderVisitor;
import fun.codedesign.yinxue.code.php2java.service.visitor.Php2JavaTokensVisitor;
import fun.codedesign.yinxue.code.php2java.service.visitor.listener.Php2JavaListener;
import fun.codedesign.yinxue.util.ResourceUtil;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenStreamRewriter;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.yinxue.swing.core.domain.FileObject;

public class Php2JavaService {
    public static void main(String[] args) throws Exception {
        toJava();
    }

    public static String phpExampleCodeMethod() {
        return ResourceUtil.getResourceAsText("manager.php");
    }

    public static String phpExampleCodeConstruct() {
        return ResourceUtil.getResourceAsText("construct.php");
    }

    public static String phpExampleCodeController() {
        return ResourceUtil.getResourceAsText("controller.php");
    }

    public static String phpExampleCodeService() {
        return ResourceUtil.getResourceAsText("service.php");
    }

    public static String phpExampleCodeModel() {
        return ResourceUtil.getResourceAsText("model.php");
    }

    public static String phpExampleCodeHelper() {
        return ResourceUtil.getResourceAsText("helper.php");
    }

    // 测试
    static void toJava() throws Exception {
        CharStream input = new ANTLRInputStream(phpExampleCodeMethod());
        PhpLexer lexer = new PhpLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PhpParser parser = new PhpParser(tokens);
        ParseTree tree = parser.htmlDocument();

        System.out.println(tree.toStringTree(parser));
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(new Php2JavaListener(), tree);
    }

    public Php2JavaContext readFileAndCreateFile(FileObject phpFile) {
        String phpText = phpFile.getText();
        Php2JavaContext javaCode = getContext(phpText);
        return javaCode;
    }

    public static Php2JavaContext getContext(String text){
        CharStream input = new ANTLRInputStream(text);
        PhpLexer lexer = new PhpLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        // 改写token输出
        TokenStreamRewriter rewriter = new TokenStreamRewriter(tokens);
        PhpParser parser = new PhpParser(tokens);
        ParseTree tree = parser.htmlDocument();
        Php2JavaTokensVisitor tokenVisitor = new Php2JavaTokensVisitor(rewriter);
        tokenVisitor.visit(tree);
        return tokenVisitor.getContext();
    }

    public static ParseTree readPhpAsAstCode(String text) {
        CharStream input = new ANTLRInputStream(text);
        PhpLexer lexer = new PhpLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        TokenStreamRewriter rewriter = new TokenStreamRewriter(tokens);
        PhpParser parser = new PhpParser(tokens);
        ParseTree tree = parser.htmlDocument();
        return tree;
    }
}
